import moment from 'moment'

export function getFirstAndLastDayTimestamps(dateString) {
    // 解析给定的日期字符串
    const date = new Date(dateString);

    // 设置为当月1号
    const firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);

    // 计算当月最后一天（下个月的0号前一天）
    const lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);

    // 获取时间戳
    const firstDayTimestamp = firstDayOfMonth.getTime();
    const lastDayTimestamp = lastDayOfMonth.getTime();

    return {
        firstDayTimestamp: firstDayTimestamp,
        lastDayTimestamp: lastDayTimestamp,
        firstTime:moment(firstDayOfMonth).format('YYYY-MM-DD'),
        lastTime:moment(lastDayOfMonth).format('YYYY-MM-DD'),
    };
}
export function getRateCount([num1,num2],index){
    if (typeof num1 !== 'number' || typeof num2 !== 'number') {
        return 0
    }
    if (typeof index !== 'number' || !Number.isInteger(index)) {
        return 0
    }
    let sum = num1 + num2;
    if (sum === 0) {
        return 0;
    }
    if (index < 0 || index > 1) {
        throw new Error('Index out of bounds');
    }
    return ([num1,num2][index]/sum*100).toFixed(0)
}
export function sliceYear(dates) {
    return dates?.map(date =>
        date
            .split("-")
            .slice(1)
            .join("-")
    );
}